package ani.trainning.cacobatph.dac;

import java.io.IOException;
import java.util.Dictionary;
import java.util.Hashtable;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import ani.trainning.cacobatph.ConstantUtil;
import ani.trainning.cacobatph.R;
import ani.trainning.cacobatph.utilities;

public class THR3_DACcomplementaire {

	private final Handler mHandler;
	private final Context context;

	String user;
	String pass;

	// constructor
	public THR3_DACcomplementaire(Handler handler, Context context,String _user, String _pass) {
		mHandler = handler;
		this.context = context;
		this.user=_user;
		this.pass=_pass;
	}

	public Thread createThread() {
		return new Thread() {
			@Override
			public void run() {
				// creation du message.
				try {
					String url = context.getString(R.string.WS4_DAC);
					Dictionary<String, String> params = new Hashtable<String, String>();
					params.put(ConstantUtil.KEY_USERNAME, user);
					params.put(ConstantUtil.KEY_PASSWORD, pass);
					String response = utilities.getContent(url, params);

					// Recuperation de la reponse et l'envoi au handler.
					Message msg=new Message();
					msg.what = ACT3_DACprincipale.RESULTAT_CORRECTE;
					msg.obj = response;
					mHandler.sendMessage(msg);
				}
				catch (IOException e) {
					Log.e("thread exeption : ", ""+e.getMessage());
					mHandler.sendEmptyMessage(ACT3_DACprincipale.EXEPTION_IO);
				}
				catch (Exception e) {
					Log.e("thread exeption : ", ""+e.getMessage());
					mHandler.sendEmptyMessage(ACT3_DACprincipale.EXEPTIONS);
				}

			}
		};
	}
}
